package com.liming.web.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liming.domain.Book;
import com.liming.domain.BuyCar;
import com.liming.domain.BuyCarItem;
import com.liming.domain.User;
import com.liming.utils.DBUtils;

public class RealClearServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			BuyCar buycar=(BuyCar)request.getSession().getAttribute("buycar");
			User user=(User)request.getSession().getAttribute("user");
			for (Map.Entry<String , BuyCarItem> entry  : buycar.getItemmap().entrySet()) {
				Book book=entry.getValue().getBook();
				int num=entry.getValue().getNum();
				String s=book.getbId()+new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date());
				if(user!=null){
					
					String sql="insert into Buylist values( \'"+user.getUsername()+"\',\'"+book.getbId()+"\',\'"+s+"\',"+(book.getbFixprice()*num)+","+num+",\'"+new Timestamp(new Date().getTime())+"\')";
					
					System.out.println(sql);
					DBUtils.insert(sql);
				}
				String sql="delete from Pickbooks where U_username=\'"+user.getUsername()+"\' and B_id=\'"+book.getbId()+"\'";
				System.out.println(sql);
				DBUtils.delete(sql);
			}
			response.sendRedirect(request.getContextPath()+"/servlet/BuyCarfoUI");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
